package com.ghck.server.bkcontroller;

import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ghck.server.utils.FileStoreUtil;
import com.ghck.shop.model.Category;
import com.ghck.shop.service.GoodsService;

@Controller
@RequestMapping(value="/system/goodCategory")
public class AdminGoodCategoryController {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(AdminGoodCategoryController.class);

	@Autowired
	private GoodsService goodsService;
	
	@ApiOperation(value="分类列表")
	@RequestMapping(value="/list")
	public String main(HttpServletRequest request){
		List<Category> list = goodsService.categoryList();
		LOGGER.info("category list:{}", JSONObject.toJSONString(list));
		request.setAttribute("list", list);
		return "back/goodCategory/list";
	}
	@ApiOperation(value="进入添加分类页面")
	@RequestMapping(value="/addInit")
	public String addCategoryInit(HttpServletRequest request){
		Object parentId = request.getParameter("parentId");
		request.setAttribute("parentId", parentId);
		return "back/goodCategory/add";
	}
	@ApiOperation(value="设置分类商家可用")
	@RequestMapping(value="/updateCategoryShopUse/{id}")
	public String updateCategoryShopUse(@PathVariable("id")Integer id){
		goodsService.updateCategoryShopUse(id);
		return "redirect:/system/goodCategory/list";
		
	}
	
	@ApiOperation(value="取消分类商家可用")
	@RequestMapping(value="/cancelCategoryShopUse/{id}")
	public String cancelCategoryShopUse(@PathVariable("id")Integer id){
		goodsService.cancelCategoryShopUse(id);
		return "redirect:/system/goodCategory/list";
	}
	
	@ApiOperation(value="添加分类")
	@RequestMapping(value="/add")
	public String addCategory(Category category,HttpServletRequest request){
		LOGGER.info("{}", JSONObject.toJSONString(category));
		goodsService.saveCategory(category);
		return "redirect:/system/goodCategory/list";
	}
	@ApiOperation(value="加入首页推荐")
	@RequestMapping(value="/addFirstHomeComment/{categoryId}" )
	public String addFirstHomeComment(@PathVariable("categoryId")Integer categoryId,HttpServletRequest request){
		goodsService.addFirstHomeComment(categoryId);
		return "redirect:/system/goodCategory/list";
	}
	
	@ApiOperation(value="取消首页推荐")
	@RequestMapping(value="/cancelFirstHomeComment/{categoryId}")
	public String cancelFirstHomeComment(@PathVariable("categoryId")Integer categoryId,HttpServletRequest request){
		goodsService.cancelFirstHomeComment(categoryId);
		return "redirect:/system/goodCategory/list";
	}
	
	@ApiOperation(value="加入热卖分类")
	@RequestMapping(value="/addHotSell/{categoryId}" )
	public String addHotSell(@PathVariable("categoryId")Integer categoryId, HttpServletRequest request){
		goodsService.addHotSell(categoryId);
		return "redirect:/system/goodCategory/list";
	}
	
	@ApiOperation(value="取消热卖分类")
	@RequestMapping(value="/cancelHotSell/{categoryId}")
	public String cancelHotSell(@PathVariable("categoryId")Integer categoryId,HttpServletRequest request){
		goodsService.cancelHotSell(categoryId);
		return "redirect:/system/goodCategory/list";
	}
	
	@ApiOperation(value="推荐到首页banner位")
	@RequestMapping(value="/addHomeBanner" ,method = RequestMethod.POST,consumes = "multipart/form-data")
	public String addHomeBanner(@RequestParam("categoryId")Integer categoryId,@RequestParam(value="file")MultipartFile file, HttpServletRequest request){
		String fileDbName = FileStoreUtil.storeFile(file, "shop/banner/");
		goodsService.addHomeBanner(categoryId,fileDbName);
		return "redirect:/system/goodCategory/list";
	}
	
	@ApiOperation(value="删除首页banner位推荐")
	@RequestMapping(value="/cancelHomeBanner/{categoryId}")
	public String cancelHomeBanner(@PathVariable("categoryId")Integer categoryId,HttpServletRequest request){
		goodsService.cancelHomeBanner(categoryId);
		return "redirect:/system/goodCategory/list";
	}
	
}
